#include "graphdaily.h"
#include "ui_graphdaily.h"
#include<QDebug>
#include<QMessageBox>
#include<QProgressBar>
#include<QDesktopWidget>
#include<QtSql/QSqlQuery>
//This shows a graph of a specific day against the categories
//If number of categories are greater than 10 , then top 10 categories are shown
GraphDaily::GraphDaily(QDate dt,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::GraphDaily)
{
    ui->setupUi(this);
    date=dt;
    qDebug()<<date;
    QRect q=QApplication::desktop()->availableGeometry(this);
    QAction* backAction = new QAction( tr("Back"), this );
    backAction->setSoftKeyRole( QAction::NegativeSoftKey );
    connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
    addAction( backAction );

    int margin=0,Awidth=0;
    Awidth=q.width();
    margin=q.width()/12.0;//For 240 width ,it is 20
    if(q.width()>500)
        margin=q.width()/18.0;

    int fontSize=6;
    ui->Vline->setGeometry(2.0*margin,margin,margin,(q.height()-(3*margin)));//Vertical

    ui->Hline->setGeometry((2.5*margin),(q.height()-(2.0*margin)),((q.width())-(3.0*margin)),margin/10.0);//Horizontal
    int avWidth=(Awidth-(ui->Vline->width()*4.0));//For 240, it is 160 available width
    int space=avWidth/40.0;//For 160 available width, it is 4
    int size=(avWidth/10.0)-space;
    int labelSize=3.3*size;
    if(q.width()>500)
        labelSize=1.9*size;
    ui->label_header->setFont(QFont( "lucida", 1.5*fontSize, QFont::Normal, TRUE));
    ui->label_header->setGeometry(1.5*margin,q.height()/2.0,q.width(),margin);
    ui->label_header->setVisible(false);
    int width=(ui->Vline->width()*3.0);

    ui->labelX->setAlignment(Qt::AlignRight);
    ui->labelX->setGeometry(0,(q.height()-(1.7*margin)),2.5*margin,1.6*size);
    ui->labelX->setFont((QFont( "lucida", fontSize, QFont::Normal, TRUE )));
    ui->labelX->setText("Category");

    ui->progressBar1->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label1->setGeometry(width,(q.height()-(1.9*margin)),size,1.5*size);
    ui->label1->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar1->width()+space;
    ui->progressBar2->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label2->setGeometry(width,(q.height()-(1.9*margin)),size,1.5*size);
    ui->label2->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar2->width()+space;
    ui->progressBar3->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label3->setGeometry(width,(q.height()-(1.9*margin)),size,1.5*size);
    ui->label3->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar3->width()+space;
    ui->progressBar4->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label4->setGeometry(width,(q.height()-(1.9*margin)),size,1.5*size);
    ui->label4->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar4->width()+space;
    ui->progressBar5->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label5->setGeometry(width,(q.height()-(1.9*margin)),size,1.5*size);
    ui->label5->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar5->width()+space;
    ui->progressBar6->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label6->setGeometry(width,(q.height()-(1.9*margin)),size,1.5*size);
    ui->label6->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar6->width()+space;
    ui->progressBar7->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label7->setGeometry(width,(q.height()-(1.9*margin)),size,1.5*size);
    ui->label7->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar7->width()+space;
    ui->progressBar8->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label8->setGeometry(width,(q.height()-(1.9*margin)),size,1.5*size);
    ui->label8->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar8->width()+space;
    ui->progressBar9->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label9->setGeometry(width,(q.height()-(1.9*margin)),size,1.5*size);
    ui->label9->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar9->width()+space;
    ui->progressBar10->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label10->setGeometry(width,(q.height()-(1.9*margin)),size,1.5*size);
    ui->label10->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    /*width=width+ui->progressBar10->width();
    ui->labelXT->setFont((QFont( "lucida", fontSize, QFont::Normal, TRUE )));
     ui->labelXT->setGeometry(width,(q.height()-(2.0*margin)),size,1.6*size);
     ui->labelXT->setText("X");*/

    ui->Vlabel1->setAlignment(Qt::AlignRight);
    ui->Vlabel2->setAlignment(Qt::AlignRight);
    ui->Vlabel3->setAlignment(Qt::AlignRight);
    ui->Vlabel4->setAlignment(Qt::AlignRight);
    ui->Vlabel5->setAlignment(Qt::AlignRight);

    ui->Vlabel1->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
    ui->Vlabel2->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
    ui->Vlabel3->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
    ui->Vlabel4->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
    ui->Vlabel5->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));

    ui->Vlabel1->setGeometry(0,margin-0.3*size,labelSize,size);
    ui->Vlabel2->setGeometry(0,((ui->Vline->height()*0.75)+ margin-0.3*size),labelSize,size);
    ui->Vlabel3->setGeometry(0,((ui->Vline->height()/2.0)+margin-0.3*size),labelSize,size);
    ui->Vlabel4->setGeometry(0,((ui->Vline->height()/4.0)+margin-0.3*size),labelSize,size);
    ui->Vlabel5->setGeometry(0.5*size,(ui->Vline->height()+margin-0.3*size),(labelSize-0.5*size),0.8*size);


    plotGraph();
}

GraphDaily::~GraphDaily()
{
    delete ui;
}
void GraphDaily::back()
{
    (new ExpDaily)->show();
}
void GraphDaily::plotGraph()
{
    db=QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("finance_db");
    if(!db.open())
    {
        QMessageBox::information(0,"Error !","Error in opning database");

    }
    else
    {
        QSqlQuery qry;
        QPair<QString,double> list1[20];
         QPair<QString,double> list2[20];
        int count=0;
        //select category,total of expenses group by date and category;
        qry.prepare(QString("select cat_name,exp_date,sum(exp_amt) from expense_table group by cat_name,exp_date having exp_date=?"));
        qry.addBindValue(date);
        qry.exec();
        for(int j=0;j<20;j++)
        {
            list1[j].first="";
            list1[j].second=0;
        }
        while(qry.next())
        {
            list1[count].first=qry.value(0).toString();
            list1[count].second=qry.value(2).toDouble();
            count++;

        }
        if(count==0)
        {
          ui->label_header->setVisible(true);
          ui->label_header->setText(tr("No expense record found"));
          ui->Vline->setVisible(false);
          ui->Hline->setVisible(false);
          ui->progressBar1->setVisible(false);
          ui->progressBar2->setVisible(false);
          ui->progressBar3->setVisible(false);
          ui->progressBar4->setVisible(false);
          ui->progressBar5->setVisible(false);
          ui->progressBar6->setVisible(false);
          ui->progressBar7->setVisible(false);
          ui->progressBar8->setVisible(false);
          ui->progressBar9->setVisible(false);
          ui->progressBar10->setVisible(false);
          ui->labelX->setVisible(false);

        }
        else
        {
            double max=0;
            for(int i=0;i<count;i++)
                list2[i].operator =(list1[i]);
            if(count>10)
            {
               QPair<QString,double> pair;
               for(int i=0;i<19;i++)
               {
                   for(int j=i+1;j<20;j++)
                   {
                   if(list1[i].second<list1[j].second)
                   {
                       pair.operator =(list1[i]);
                      list1[i].operator =(list1[j]);
                      list1[j].operator =(pair);
                   }
               }
               }//Sorting for finding top 10 categories

               int j=0;int index=0;
               for(int i=0;i<19;i++)
               {
                   for(j=0;j<10;j++)
                   {
                       if((list2[i].first.compare(list1[j].first)==0)&&(list2[i].second==list1[j].second))
                       {
                           pair.operator =(list2[index]);
                           list2[index].operator =(list2[i]);
                           list2[i].operator =(pair);
                           index++;
                           break;
                       }
                   }
               }
            }

        for(int i=0;i<10;i++)
        {
            if(list2[i].second>max)
                max=list2[i].second;
        }
        qDebug()<<max;



        ui->progressBar1->setMaximum(max);
        ui->progressBar2->setMaximum(max);
        ui->progressBar3->setMaximum(max);
        ui->progressBar4->setMaximum(max);
        ui->progressBar5->setMaximum(max);
        ui->progressBar6->setMaximum(max);
        ui->progressBar7->setMaximum(max);
        ui->progressBar8->setMaximum(max);
        ui->progressBar9->setMaximum(max);
        ui->progressBar10->setMaximum(max);

        ui->progressBar1->setValue(list2[0].second);
        ui->progressBar2->setValue(list2[1].second);
        ui->progressBar3->setValue(list2[2].second);
        ui->progressBar4->setValue(list2[3].second);
        ui->progressBar5->setValue(list2[4].second);
        ui->progressBar6->setValue(list2[5].second);
        ui->progressBar7->setValue(list2[6].second);
        ui->progressBar8->setValue(list2[7].second);
        ui->progressBar9->setValue(list2[8].second);
        ui->progressBar10->setValue(list2[9].second);



        ui->label1->setText(QString("%1").arg(list2[0].first.mid(0,2)));
        ui->label2->setText(QString("%1").arg(list2[1].first.mid(0,2)));
        ui->label3->setText(QString("%1").arg(list2[2].first.mid(0,2)));
        ui->label4->setText(QString("%1").arg(list2[3].first.mid(0,2)));
        ui->label5->setText(QString("%1").arg(list2[4].first.mid(0,2)));
        ui->label6->setText(QString("%1").arg(list2[5].first.mid(0,2)));
        ui->label7->setText(QString("%1").arg(list2[6].first.mid(0,2)));
        ui->label8->setText(QString("%1").arg(list2[7].first.mid(0,2)));
        ui->label9->setText(QString("%1").arg(list2[8].first.mid(0,2)));
        ui->label10->setText(QString("%1").arg(list2[9].first.mid(0,2)));

        int maxi=max;
        qDebug()<<max;
        qDebug()<<maxi;

        ui->Vlabel1->setText(QString("%1").arg(maxi));
        ui->Vlabel2->setText(QString("%1").arg(maxi/4.0));
        ui->Vlabel3->setText(QString("%1").arg(maxi/2.0));
        ui->Vlabel4->setText(QString("%1").arg(maxi*0.75));
        ui->Vlabel5->setText("0");

    }//else


        db.close();
    }//connection
}
